<?php
/**首页
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/12
 * Time: 15:10
 */
namespace app\adminson\controller;

use app\adminson\controller\Base;
use app\common\model\ManagerDuty as ManagerDutyModel;
use app\common\model\NodeSon as NodeSonModel;
use app\common\model\UserTypeSon;
use think\Db;

/**
 * 职位管理控制器
 * Class Index
 * @package app\index\controller
 */
class Duty extends Base
{
    /**
     * [getIndex  获取所有职位]
     * @author [C。]
     * @return json
     */
    public function getIndex()
    {
        $model = new ManagerDutyModel();

        $data = input('param.');
        $map = [];
        $map['comid'] = session('comid');
        $map['status'] = ['neq',-1];

        $this->getPageAndSize($data);
        try {
            $count = $model->getCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size);
        } catch (\Exception $e) {
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postAdd  添加职位]
     * @author [C。]
     * @return json
     */
    public function postAdd()
    {
        $model = new ManagerDutyModel();
        $param = input('post.');
        $param['comid'] = session('comid');
        try{
            $model->addData($param);
        }catch(\Exception $e){
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1,'添加成功');
    }
    /**
     * [postEdit  修改职位]
     * @author [C。]
     * @return json
     */
    public function postEdit()
    {
        $model = new ManagerDutyModel();
        $param = input('post.');
        try{
            $model->editData($param);
        }catch(\Exception $e){
            return admin_json(-1, '修改失败'.$e->getMessage());
        }
        return admin_json(1,'修改成功');
    }
    /**
     * [getDel  删除职位]
     * @author [C。]
     * @return json
     */
    public function getDel()
    {
        $model = new ManagerDutyModel();
        $param = input('param.');
        try{
            $model->deleteFalseData(['id'=>$param['id']],['status'=>-1]);
        }catch(\Exception $e){
            return admin_json(-1, '删除失败'.$e->getMessage());
        }
        return admin_json(1,'删除成功');
    }
    /**
     * [giveAccess 分配权限]
     * @return [type] [description]
     * @author [C。]
     */
    public function getAccess()
    {
        $param = input('param.');

        $node = new NodeSonModel();
        //获取现在的权限
        if('get' == $param['type']){
            $nodeStr = $node->getNode($param['id']);

            return admin_json(1,'success',$nodeStr);
        }
        //分配新权限
        if('give' == $param['type']){
            $doparam = [
                'id' => $param['id'],
                'rules' => $param['rule'],
                'duty' =>  $param['duty']
            ];
            $user = new UserTypeSon();
            $res = $user->editAccess($doparam);

            return admin_json(1,'角色权限分配成功');
        }
    }
}